package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @ClassName: DishServiceImpl
 * @Author: xmc
 * @Date: 2022/8/16 20:46
 * @Version: 1.0
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     *
     *方法说明:新增菜品，同时保存口味信息
     * @author xmc
     * @param dishDto
     * @return void
     * @date 2022/8/17 18:22
     */
    @Override
    @Transactional
    public void saveWithDishFlavor(DishDto dishDto) {
       
        //保存菜品基本信息
        this.save(dishDto);
        
        //保存菜品口味信息，并加入dishId
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存口味信息
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto getByIdWithDishFlavor(Long id) {
        //通过id获取菜品信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        //通过dishId查口味信息
        List<DishFlavor> dishFlavors = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(dishFlavors);
        return dishDto;

    }

    @Override
    public void updateWithDishFlavor(DishDto dto) {
        this.updateById(dto);
        //先全部删除口味，再更新，防止在原理的基础上再加口味，或减少
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dto.getId());
        dishFlavorService.remove(wrapper);
        //更新

        List<DishFlavor> flavors = dto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dto.getId());
            return item;
        }).collect(Collectors.toList());


        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public void deleteWithDishFlavor(String id) {
        Dish dish = this.getById(id);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorService.remove(wrapper);
        this.removeById(id);

    }
}
